package com.kobi.controller;

import java.io.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.kobi.connection.JDBCUtils;
import com.kobi.data.Book;

/**
 * Servlet implementation class loginservlet
 */
@WebServlet("/loginservlet")
public class loginservlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public loginservlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
        try {
        	conn=JDBCUtils.getConnection();
        	String sql="select * from student where username=?&&password=?";
        	pstmt=conn.prepareStatement(sql);
        	pstmt.setString(1, username);
        	pstmt.setString(2,password);
        	rs=pstmt.executeQuery();
        	if(rs.next())
        	{
        		request.getRequestDispatcher("/powerchose.html").forward(request,response);
        	}
        	else
        		request.getRequestDispatcher("/loselogin.jsp").forward(request,response);
            
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //6. 释放资源
            JDBCUtils.close(rs,pstmt,conn);
        }
	}
	}
